package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.AuthorCollectSingleGoodActivity;
import com.wangtao.firstapp.activity.EditorWishActivity;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.modles.TwoSifeSecond;
import com.wangtao.firstapp.utils.ToastUtils;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * 二级页面-->小唐君有图页面（主内容 适配器）
 * Created by lanouhn on 16/9/2.
 */
public class ACTSifeSecondContentAdapter extends RecyclerView.Adapter<ACTSifeSecondContentAdapter.ContentViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private Context context;
    private TwoSifeSecond twoSifeSecond;

    //标志位
    private int flag=0;

    public ACTSifeSecondContentAdapter(Context context, TwoSifeSecond twoSifeSecond) {
        this.context = context;
        this.twoSifeSecond = twoSifeSecond;
    }
    //创建自定义Toast，并实例化
    ToastUtils toastUtils = new ToastUtils(context);

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.actsifesecond_two_item_content, null);
        ContentViewHolder holder = new ContentViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(final ContentViewHolder holder, final int position) {
        TwoSifeSecond.DataBean dataBean = twoSifeSecond.getData();
        final List<TwoSifeSecond.DataBean.ProductListBean> productListBean = twoSifeSecond.getData().getProduct_list();

        if (position == 0) {

            holder.linearLayout_view_one.setVisibility(View.VISIBLE);
            holder.title.setText(dataBean.getTitle());
            holder.author.setText(dataBean.getUser().getNickname());
            holder.views.setText(dataBean.getViews());
            holder.content.setText(dataBean.getDesc());
            Picasso.with(context).load(dataBean.getPic()).into(holder.imageViewHead);
            Picasso.with(context).load(dataBean.getUser().getAvatar()).into(holder.logo);
        } else {
            int i = position - 1;

            holder.linearLayout_view_two.setVisibility(View.VISIBLE);
            holder.caption.setText(productListBean.get(i).getTitle());
            holder.matter.setText(productListBean.get(i).getDesc());
            holder.tv1.setText(productListBean.get(i).getTitle());
            holder.price.setText(productListBean.get(i).getPrice());

            holder.tvLikes.setText(productListBean.get(i).getLikes());

            if (holder.linearLayout_imageView.getChildCount() > 0)
                holder.linearLayout_imageView.removeAllViews();

            for (int j = 0; j < productListBean.get(i).getPics().size(); j++) {
                ImageView imageView = new ImageView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(400), DensityUtil.dip2px(400))//图片大小
                        //.setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .build();

                imageView.setPadding(0, 0, 0, 15);
                x.image().bind(imageView, productListBean.get(i).getPics().get(j).getUrl(), imageOptions);
                holder.linearLayout_imageView.addView(imageView);
            }
            Picasso.with(context).load(productListBean.get(i).getThumbnail_pic()).into(holder.ivShop);
        }

        //点击头像或作者,进入作者介绍页面
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = twoSifeSecond.getData().getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

        //点击头像或作者,进入作者介绍页面
        holder.author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = twoSifeSecond.getData().getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

        //点击图片或卖东西，进入单品详情页面***这是另一种单品详情页面
        holder.linearLayout_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取商品ID
                String shop_ID = twoSifeSecond.getData().getProduct_list().get(position-1).getId();
                Intent intent1 = new Intent(context, AuthorCollectSingleGoodActivity.class);
                intent1.putExtra("shop_iD", shop_ID);
                context.startActivity(intent1);
            }
        });

        //点击图片或卖东西，进入单品详情页面***这是另一种单品详情页面
        holder.relativeLayout_skip_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取商品ID
                String shop_ID = twoSifeSecond.getData().getProduct_list().get(position-1).getId();
                Intent intent1 = new Intent(context, AuthorCollectSingleGoodActivity.class);
                intent1.putExtra("shop_iD", shop_ID);
                context.startActivity(intent1);
            }
        });

        //点击 ❤ 收入心愿单,切换，并弹出Dialog
        holder.relativeLayout_view_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag==0){
                    flag=1;
                    holder.ivWish.setVisibility(View.VISIBLE);
                    holder.tvWish.setVisibility(View.VISIBLE);
                    holder.ivLikes.setVisibility(View.GONE);
                    holder.tvLikes.setVisibility(View.GONE);

                    Toast toast = new Toast(context);
                    View view1 = LayoutInflater.from(context).inflate(R.layout.likes_wish_toast, null);
                    ImageView logoPhoto = (ImageView) view1.findViewById(R.id.logoPhoto_likes_wish_toast);
                    Picasso.with(context).load(productListBean.get(position-1).getPic()).into(logoPhoto);
                    toast.setGravity(Gravity.BOTTOM, 0, 20);
                    toast.setView(view1);
                    toast.show();
                }else {
                    AlertDialog.Builder aBuilder= new AlertDialog.Builder(context);
                    View view = LayoutInflater.from(context).inflate(R.layout.likes_wish_dialog, null);
                    aBuilder.setView(view);
                    aBuilder.setCancelable(false);
                    final AlertDialog alertDialog = aBuilder.create();

                    ImageView bad = (ImageView) view.findViewById(R.id.bad_likes_wish_dialog);//右上角的“X”，点击退出Dialog
                    //容器，放置“我的喜欢”。点击后退出Dialog,toast
                    LinearLayout lltLike = (LinearLayout) view.findViewById(R.id.lltLike_likes_wish_dialog);
                    Button cancel = (Button) view.findViewById(R.id.cancel_likes_wish_dialog);//点击后，取消喜欢，并退出Dialog
                    TextView found = (TextView) view.findViewById(R.id.found_likes_wish_dialog);//点击后，跳转至 编辑心愿单页面
                    ImageView shopPhoto = (ImageView) view.findViewById(R.id.shopPhoto_likes_wish_dialog);//商品图片，获取显示
                    Picasso.with(context).load(productListBean.get(position-1).getPic()).into(shopPhoto);

                    //退出Dialog
                    bad.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.cancel();
                        }
                    });

                    //退出Dialog，在Activity中写取消喜欢
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.ivWish.setVisibility(View.GONE);
                            holder.tvWish.setVisibility(View.GONE);
                            holder.ivLikes.setVisibility(View.VISIBLE);
                            holder.tvLikes.setVisibility(View.VISIBLE);
                            flag = 0;
                            alertDialog.cancel();
                        }
                    });

                    //退出Dialog，并Toast
                    lltLike.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast toast = new Toast(context);
                            View view1 = LayoutInflater.from(context).inflate(R.layout.likes_wish_toast, null);
                            ImageView logoPhoto = (ImageView) view1.findViewById(R.id.logoPhoto_likes_wish_toast);
                            Picasso.with(context).load(productListBean.get(position-1).getPic()).into(logoPhoto);
                            toast.setGravity(Gravity.BOTTOM, 0, 20);
                            toast.setView(view1);
                            toast.show();

                            alertDialog.cancel();
                        }
                    });

                    //跳转至，编辑心愿单页面
                    found.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(context, EditorWishActivity.class);
                            context.startActivity(intent);

                        }
                    });
                    alertDialog.show();
                }
            }
        });

        //点击 购买，进入宝贝详情页面
        holder.linearLayout_skip_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String urlAlter= productListBean.get(position-1).getUrl();
                Uri uriTb = Uri.parse(urlAlter);
                Intent intent = new Intent(Intent.ACTION_VIEW, uriTb);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return twoSifeSecond.getData().getProduct_list().size() + 1;
    }

    public class ContentViewHolder extends RecyclerView.ViewHolder {

        private TextView title, author, views, content, caption, matter, tv1, price,  tvWish,tvLikes;
        private ImageView imageViewHead, logo, ivShop, imageview, likes_image,ivWish,ivLikes;

        private RelativeLayout relativeLayout_skip_one, relativeLayout_view_one;
        private LinearLayout linearLayout_view_one, linearLayout_view_two, linearLayout_skip_one, linearLayout_imageView;

        public ContentViewHolder(View itemView) {
            super(itemView);

            linearLayout_view_one = (LinearLayout) itemView.findViewById(R.id.linearLayout_view_one);
            title = (TextView) itemView.findViewById(R.id.title_actSifeSecond_two_item_one);
            author = (TextView) itemView.findViewById(R.id.author_actSifeSecond_two_item_one);
            views = (TextView) itemView.findViewById(R.id.views_actSifeSecond_two_item_one);
            content = (TextView) itemView.findViewById(R.id.content_actSifeSecond_two_item_one);

            imageViewHead = (ImageView) itemView.findViewById(R.id.imageViewHead_actSifeSecond_two_item_one);
            logo = (ImageView) itemView.findViewById(R.id.logo_actSifeSecond_two_item_one);

            relativeLayout_view_one = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_view_one);
            linearLayout_imageView = (LinearLayout) itemView.findViewById(R.id.linearLayout_imageView);
            linearLayout_view_two = (LinearLayout) itemView.findViewById(R.id.linearLayout_view_two);
            relativeLayout_skip_one = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_skip_one);
            linearLayout_skip_one = (LinearLayout) itemView.findViewById(R.id.linearLayout_skip_one);
            caption = (TextView) itemView.findViewById(R.id.caption_actSifeSecond_two_item_one);
            matter = (TextView) itemView.findViewById(R.id.matter_actSifeSecond_two_item_one);
            tv1 = (TextView) itemView.findViewById(R.id.tv1_actSifeSecond_two_item_one);
            price = (TextView) itemView.findViewById(R.id.price_actSifeSecond_two_item_one);

            //点击后
            tvWish = (TextView) itemView.findViewById(R.id.textView_actSifeSecond_two_item_one);
            ivWish= (ImageView) itemView.findViewById(R.id.imageview_actSifeSecond_two_item_one_like);
            //点击前
            tvLikes = (TextView) itemView.findViewById(R.id.likes_actSifeSecond_two_item_one);
            ivLikes= (ImageView) itemView.findViewById(R.id.likes_actSifeSecond_two_item_one_view);

            ivShop = (ImageView) itemView.findViewById(R.id.ivShop_actSifeSecond_two_item_one);

        }
    }
}
